﻿using System;
using ZProjectNamespaceNameZ.ZSafeParentProjectNameZ;
using System.Data;
using MbUnit.Framework;
using System.Collections.Generic;

namespace ZSafeProjectNameZ.Tests
{
    /// <summary>
    /// Summary description for SqlDataProviderFixture
    /// </summary>
    [TestFixture]
    public class SqlDataProviderFixture
    {
        #region Fields/Properties
        //private const int INT_ModuleID = 321;
        private const string STR_Content = "Simple Sample Content";
        private const string STR_UpdatedContent = "Updated Content";
        private const string STR_COLUMN_NAME_Content = "Content";

        private List<int> MockModules;
        private List<int> MockUserIds;
        private SqlDataProvider sqlDataProvider;
        #endregion

        #region Setup/TearDown
        private Boolean scriptsApplied = false;
        [FixtureSetUp]
        public void TestSetup()
        {
            // SetUp Test Database
            if (!scriptsApplied)
            {
                // NOTE: It appears that MbUnit runs the FixtureSetUp twice (with DevExpress Unit Test Runner),
                //       but we want to preserve the database changes so that they can be manually checked if
                //       needed.
                DatabaseManager.ReApplyScripts();
                scriptsApplied = true;
            }
            MockModules = DatabaseManager.GetMockModules();
            MockUserIds = DatabaseManager.GetMockUserIds();

            // SetUp SqlDataProvider
            sqlDataProvider = new SqlDataProvider();
            sqlDataProvider.ConnectionString = DatabaseEnvironment.ConnectionString;
            sqlDataProvider.ObjectQualifier = DatabaseEnvironment.ObjectQualifier;
            sqlDataProvider.DatabaseOwner = DatabaseEnvironment.DatabaseOwner;
        }
        #endregion

        [Test]
        public void MockModuleIDsShouldExist()
        {
            Assert.IsNotNull(MockModules);
            Assert.GreaterThan<int>(MockModules.Count, 0);
        }

        [Test]
        public void MockUserIDsShouldExist()
        {
            Assert.IsNotNull(MockUserIds);
            Assert.GreaterThan<int>(MockUserIds.Count, 0);
        }

        [Test]
        [Description("Adds and retrieves a TestDrivenModule record in the database")]
        public void AddTestDrivenModule()
        {
            // Arrange
            string actualContent = "";

            // Act - Add
            int itemId = sqlDataProvider.AddZParentProjectNameZ(MockModules[0], STR_Content, MockUserIds[0]);

            // Assert - Add
            Assert.IsTrue(itemId > 0);

            // Act - Read
            using (IDataReader dataReader = sqlDataProvider.GetZParentProjectNameZ(MockModules[0], itemId))
            {
                // Assert - Read
                Assert.IsTrue(dataReader.Read());
                // Act - Module Content
                actualContent = (string)dataReader[STR_COLUMN_NAME_Content];
                // Assert - Module Content
                Assert.AreEqual<string>(STR_Content, actualContent);
            }
        }

        [Test]
        [Description("Adds, retrieves and then deletes a TestDrivenModule record in the database")]
        public void DeleteTestDrivenModule()
        {
            string content = "";

            int itemId = sqlDataProvider.AddZParentProjectNameZ(MockModules[0], STR_Content, MockUserIds[0]);
            Assert.IsTrue(itemId > 0);

            using (IDataReader dataReader = sqlDataProvider.GetZParentProjectNameZ(MockModules[0], itemId))
            {
                if (dataReader.Read())
                    content = (string)dataReader[STR_COLUMN_NAME_Content];
            }
            Assert.AreEqual<string>(STR_Content, content);

            sqlDataProvider.DeleteZParentProjectNameZ(MockModules[0], itemId);

            using (IDataReader dataReader = sqlDataProvider.GetZParentProjectNameZ(MockModules[0], itemId))
            {
                Assert.IsFalse(dataReader.Read());
            }
        }

        [Test]
        [Description("Adds, updates and retrieves a TestDrivenModule record in the database")]
        public void UpdateZParentProjectNameZ()
        {
            string content = "";

            int itemId = sqlDataProvider.AddZParentProjectNameZ(MockModules[0], STR_Content, MockUserIds[0]);
            Assert.IsTrue(itemId > 0);

            sqlDataProvider.UpdateZParentProjectNameZ(MockModules[0], itemId, STR_UpdatedContent, MockUserIds[0]);

            using (IDataReader dataReader = sqlDataProvider.GetZParentProjectNameZ(MockModules[0], itemId))
            {
                if (dataReader.Read())
                    content = (string)dataReader[STR_COLUMN_NAME_Content];
            }
            Assert.AreEqual<string>(STR_UpdatedContent, content);
        }

        [Test]
        [Description("Adds 2 and then retrieves 2 TestDrivenModule records in the database")]
        public void GetZParentProjectNameZs()
        {
            int records = 0;

            int itemId1 = sqlDataProvider.AddZParentProjectNameZ(MockModules[0], STR_Content, MockUserIds[0]);
            int itemId2 = sqlDataProvider.AddZParentProjectNameZ(MockModules[0], STR_Content, MockUserIds[0]);
            Assert.IsTrue(itemId1 > 0);
            Assert.IsTrue(itemId2 > 0);

            using (IDataReader dataReader = sqlDataProvider.GetZParentProjectNameZs(MockModules[0]))
            {
                while (dataReader.Read())
                {
                    int actualId = (int)dataReader["ItemId"];
                    if (actualId == itemId1 || actualId == itemId2)
                        records++;
                }
            }

            Assert.AreEqual<int>(2, records);
        }
    }
}
